package com.Serverce;

import com.DAO.Administorinfo;
import com.Util.Window;
import com.Module.Administor;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class Login {
    public Login(){
        JFrame jf = new Window().window("酒店管理系统",500,400,500,400);
        Box choic=Box.createHorizontalBox();
        JLabel choic_jlable=new JLabel("身份选择");
        JRadioButton jr1=new JRadioButton("管理员");
        JRadioButton jr2=new JRadioButton("用户");
        ButtonGroup buttonGroup=new ButtonGroup();
        buttonGroup.add(jr1);
        buttonGroup.add(jr2);
        choic.add(choic_jlable);
        choic.add(Box.createHorizontalStrut(20));
        choic.add(jr1);
        choic.add(Box.createHorizontalStrut(38));
        choic.add(jr2);

        Box idBox=Box.createHorizontalBox();
        JLabel idlLabel=new JLabel("编        号");
        JTextField iField=new JTextField(15);

        idBox.add(idlLabel);
        idBox.add(Box.createHorizontalStrut(20));
        idBox.add(iField);

        Box uBox=Box.createHorizontalBox();
        JLabel uLabel=new JLabel("姓        名");
        JTextField uField=new JTextField(15);

        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);

        Box pBox=Box.createHorizontalBox();
        JLabel pLabel=new JLabel("密        码");
        JTextField pField=new JTextField(15);

        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(pField);

        Box btnbox=Box.createHorizontalBox();
        JButton logiButton=new JButton("登        录");
        logiButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String state=jr1.isSelected()?"管理员":"用户";
                String id=iField.getText();
                String name=uField.getText();
                String password=pField.getText();
                if (iField.getText().equals("") || uField.getText().equals("") || pField.getText().equals("")) {
                    JOptionPane.showMessageDialog(null,"不能输入空值","错误",JOptionPane.ERROR_MESSAGE);
                }else {
                    if (state.equals("管理员")) {
                        Object obj= new Administorinfo().select(id).get(0);
                        Administor administor=(Administor) obj;
                        if (id.equals(String.valueOf(administor.getId())) && name.equals(administor.getName()) && password.equals(administor.getPassword())) {
                            new Administorview();
                            jf.dispose();
                        }else {
                            JOptionPane.showMessageDialog(null,"用户名或密码错误","错误",JOptionPane.ERROR_MESSAGE);
                        }

                    }else {
                        JOptionPane.showMessageDialog(null,"非管理人员！","错误",JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        JButton registButton=new JButton("用户添加");
        registButton.addActionListener(e->{new Enroll();jf.dispose();});

        btnbox.add(logiButton);
        btnbox.add(Box.createHorizontalStrut(50));
        btnbox.add(registButton);

        Box vBox=Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(70));
        vBox.add(choic);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(idBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnbox);

        JPanel jp=new JPanel();
        jp.add(vBox);
        jf.add(jp);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new Login();
    }
}
